package org.bite.lesson09;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo904 {
    public static void main(String[] args) throws InterruptedException {
        //定义一个线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5), new ThreadPoolExecutor.CallerRunsPolicy());

        //通过循环向线程池中提交任务
        for (int i = 0; i < 100; i++) {
            int taskId = i + 1;
            threadPool.submit(() -> {
                System.out.println("执行任务：" + taskId + "," + Thread.currentThread().getName());
            });
            if(taskId % 9 == 0){
                TimeUnit.SECONDS.sleep(2);
            }

        }
    }
}
